import { SelectUserComponentServiceProvider } from '@src/component/common/BaseSelectUser/model';
/* model */
import { SelectUserComponentDepartment } from '@src/component/common/BaseSelectUser/model'
import { BaseSelectUserBreadCrumbItem } from '@src/component/common/BaseSelectUser/components/BreadCrumb/model'
/* store */
import {
  useBaseSelectUserStoreCurrentDepartmentNav,
  useBaseSelectUserStoreCurrentServiceProviderNav,
  useBaseSelectUserStoreDepartmentNavList,
  useBaseSelectUserStoreServiceProviderNavList
} from '@src/component/common/BaseSelectUser/store'
/* vue */
import { computed, ComputedRef } from 'vue'
/* types */
import { isEmpty, isFalse, isNotEmpty } from '@src/util/type'

/** 
 * @description 部门导航列表
*/
function useDepartmentNavList() {
  
  const { departmentNavList, setDepartmentNavList } = useBaseSelectUserStoreDepartmentNavList()
  const { setCurrentDepartmentNav } = useBaseSelectUserStoreCurrentDepartmentNav()
  
  const departmentNavListForBreadCrumb: ComputedRef<BaseSelectUserBreadCrumbItem[]> = computed(() => {
    return departmentNavList.value.map(item => {
      return {
        text: item.name,
        value: item.id
      }
    })
  })
  
  const isDepartmentNavListEmpty = computed(() => {
    return isEmpty(departmentNavList.value)
  })
  
  const isDepartmentNavListNotEmpty = computed(() => {
    return isNotEmpty(departmentNavList.value)
  })
  
  const isRootNav = computed(() => {
    return isDepartmentNavListEmpty.value
  })
  
  const pushNav = (nav: SelectUserComponentDepartment) => {
    setCurrentDepartmentNav(nav)
    setDepartmentNavList([...departmentNavList.value, nav])
  }
  
  const popNav = (nav: SelectUserComponentDepartment) => {
    
    setCurrentDepartmentNav(nav)
    
    let index = departmentNavList.value.findIndex(item => item.id === nav?.id)
    
    const isLast = index === departmentNavList.value.length - 1
    const isFirst = index == 0
    const isMiddleIndex = isFalse(isFirst) && isFalse(isLast)
    
    if (index > -1) {
      
      if (isMiddleIndex) {
        ++index
      }
      
      const navList = departmentNavList.value.slice(0, index)
      
      setDepartmentNavList(navList)
    }
    
  }
  
  return {
    departmentNavList,
    departmentNavListForBreadCrumb,
    isDepartmentNavListEmpty,
    isDepartmentNavListNotEmpty,
    isRootNav,
    setCurrentDepartmentNav,
    setDepartmentNavList,
    pushNav,
    popNav
  }
}

/**
* @description 服务商导航列表
*/
function useServiceProviderNavList() {
  
  const {
    serviceProviderNavList, 
    isServiceProviderNavListEmpty, 
    isServiceProviderNavListNotEmpty, 
    setServiceProviderNavList 
  } = useBaseSelectUserStoreServiceProviderNavList()
  
  const { setCurrentServiceProviderNav, currentServiceProviderNav, isCurrentServiceProviderNavEmpty, isCurrentServiceProviderNavNotEmpty } = useBaseSelectUserStoreCurrentServiceProviderNav()
  
  const serviceProviderNavListForBreadCrumb: ComputedRef<BaseSelectUserBreadCrumbItem[]> = computed(() => {
    return serviceProviderNavList.value.map(item => {
      return {
        text: item.name,
        value: item.id
      }
    })
  })
  
  const isRootNav = computed(() => {
    return isServiceProviderNavListEmpty.value
  })
  
  const pushNav = (nav: SelectUserComponentServiceProvider) => {
    setCurrentServiceProviderNav(nav)
    setServiceProviderNavList([...serviceProviderNavList.value, nav])
  }
  
  const popNav = (nav: SelectUserComponentServiceProvider) => {
    
    setCurrentServiceProviderNav(nav)
    
    let index = serviceProviderNavList.value.findIndex(item => item.id === nav?.id)
    
    const isLast = index === serviceProviderNavList.value.length - 1
    const isFirst = index == 0
    const isMiddleIndex = isFalse(isFirst) && isFalse(isLast)
    
    if (index > -1) {
      
      if (isMiddleIndex) {
        ++index
      }
      
      const navList = serviceProviderNavList.value.slice(0, index)
      
      setServiceProviderNavList(navList)
    }
    
  }
  
  return {
    serviceProviderNavList,
    serviceProviderNavListForBreadCrumb,
    isServiceProviderNavListEmpty,
    isServiceProviderNavListNotEmpty,
    isRootNav,
    currentServiceProviderNav, 
    isCurrentServiceProviderNavEmpty, 
    isCurrentServiceProviderNavNotEmpty,
    setCurrentServiceProviderNav,
    pushNav,
    popNav,
    setServiceProviderNavList
  }
}

export {
  useDepartmentNavList,
  useServiceProviderNavList
}
